package com.tanhua.app.controller;

import com.tanhua.app.Service.CommentService;
import com.tanhua.app.Service.MovementService;
import com.tanhua.app.interceptor.UserThreadLocal;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;
    @Autowired
    private CommentService commentService;
   // POST/movements
    //动态-发布
@PostMapping
    public ResponseEntity  movements(Movement movement, MultipartFile[] imageContent) throws IOException {
    movementService.publishMovement(movement,imageContent);
    return ResponseEntity.ok(null);
}

    //GET/movements/all
    //我的动态
    @GetMapping("/all")
    public ResponseEntity all(Long userId,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int pageSize){
 PageResult<MovementsVo>pageResult= movementService.all(userId, page,pageSize);
    return ResponseEntity.ok(pageResult);

    }

    // GET/movements
    // 好友动态
    @GetMapping
    public ResponseEntity Friendmovements(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10")int pageSize){
  PageResult<MovementsVo> pr=movementService.findFriendMovements(page,pageSize);
  return ResponseEntity.ok(pr);

    }

    //GET/movements/recommend
    //推荐动态
    @GetMapping("/recommend")
    public ResponseEntity recommendMovements(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10")int pageSize){
        PageResult<MovementsVo> pr=movementService.findRecommendMovements(page,pageSize);
        return ResponseEntity.ok(pr);

    }
    //GET/movements/:id
    //根据动态id查询单条动态
    @GetMapping("/{id}")
    public ResponseEntity movementsById(@PathVariable("id") String id){
  MovementsVo movementsVo= movementService.findMovementsByid(id);
  return ResponseEntity.ok(movementsVo);

    }

    //GET/movements/:id/like
    //点赞
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.likeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    //GET/movements/:id/dislike
    //取消动态点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String movementId){
     Integer likeCount=  commentService.disLike(movementId);
    return ResponseEntity.ok(likeCount);
    }

    //GET/movements/:id/love
    //动态喜欢
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId){
     Integer loveCount=  commentService.love(movementId);
     return ResponseEntity.ok(loveCount);

    }

   // GET/movements/:id/unlove
    //动态取消喜欢
   @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId){
    Integer loveCount=  commentService.unlove(movementId);
     return ResponseEntity.ok(loveCount);
   }

//    接口名称：谁看过我
//    接口路径：GET/movements/visitors

    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        Long userId = UserThreadLocal.getid();
      List<VisitorsVo>  visitorsVos= movementService.QueryVisitorsList();

    return ResponseEntity.ok(visitorsVos);
    }
}
